﻿@{
    var singlelineDataTypes = new string[] { "MultilineText", "ImageUrl" };
    FormCollection filters = ViewBag.FilterForm as FormCollection;

}
@if(ViewData.TemplateInfo.TemplateDepth > 1) {
    @: @ViewData.ModelMetadata.SimpleDisplayText
} else {
    int cols = 2;
    int curr = 0;
    int colspan = 1;
    var props = ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForFilter() && !ViewData.TemplateInfo.Visited(pm) && !pm.IsComplexType);
    int totalCols = props.Count();
    if (totalCols > 0)
    {
        Html.EnableClientValidation(false);
    <table id="filter-table" class="editor-table" cellpadding="0" cellspacing="0" border="0">
        @foreach (var prop in props)
        {
            colspan = 1;
            if (singlelineDataTypes.Contains(prop.DataTypeName))
            {
                colspan = 3;
                if (curr % cols == cols - 1 || curr == totalCols - 1)
                {
                    curr++;
                @:</tr>
                }
            }
            if (curr % cols == 0)
            {
                if (singlelineDataTypes.Contains(prop.DataTypeName))
                {
                    curr++;
                }
                @:<tr>
            }
                <td class="caption">
                    <div class="editor-label" style="text-align: right;">
                        @Html.Label(prop.PropertyName)
                    </div>
                </td>
                <td colspan="@colspan">
                    <div class="editor-field">
                        @Html.Editor(prop.PropertyName, prop.FilterHint(), new { data = this.Model })
                    </div>
                </td>
                   if (curr % cols == cols - 1 || curr == totalCols - 1)
                   {
            @:</tr>
            }
                   curr++;
        }
    </table>
    }
}


<script type="text/javascript">
    $(function () {
        $('#filter-table tr').each(function () {
            if ($('td', this).length == 4)
                return;
            $('td', this).eq(1).attr('colspan', '3');
        });
        $('#filter-table td[colspan=3] input').css('width', '97%');
@if(filters != null && filters.Count > 0)
{
    foreach (var key in filters)
    {
        <text>
        $('#filter-table #@key.ToString()').val('@filters[key.ToString()]');
        </text>
    }
}    
    });
</script>